package com.neuedu.javase.io;

import java.io.*;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.io 
 * date:   2025/11/5   16:03  
 * 描述:   读取文件
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class MyInputStream {

    //要求得有文件
    public static final  String  fileName = "D:\\upload\\a.txt";
    public static final  String  fileName2 = "D:\\upload\\b.txt";


    /**
     * 读取一个字节
     */
    public void readOneByte(){
        //创建文件句柄
        File file = new File(fileName);



        //使用InputStream 读取文件
        try {
            InputStream is = new FileInputStream(file);


            //读取单个字节
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());
            System.out.println((char)is.read());





        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 使用缓冲区
     */

    public  static void readOneBuffer(){
        //创建文件句柄
        File file = new File(fileName2);



        //使用InputStream 读取文件
        try {
            InputStream is = new FileInputStream(file);

            byte[] buffer = new byte[1024];
            int len  = 0;
            // System.out.print(new String(buffer));
            while((len = is.read(buffer)) != -1){
                System.out.print(new String(buffer,0,len));
            }






        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        readOneBuffer();




    }


}
